<template>
	<!-- 类别管理 -->
	<p-modal
			:title="title"
			:width="width"
			:visible="visible"
			:confirmLoading="confirmLoading"
			@ok="handleOk"
			@cancel="handleCancel"
			cancelText="关闭">
		<p-spin :spinning="confirmLoading">
			<p-form :form="form">

				<p-row>
					<p-col  :span="24" style="display:none" >
						<p-form-item label="不良组编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
							<p-input readOnly  v-decorator="[ 'badGroupBillno', validatorRules.badGroupBillno]" placeholder="不良组编码"></p-input>
						</p-form-item>
					</p-col>

					<p-col  :span="24">
						<p-form-item label="问题组别" :labelCol="labelCol" :wrapperCol="wrapperCol">
							<j-dict-select-tag  type="list" v-decorator="['badGroupName', validatorRules.badGroupName]" :trigger-change="true" dictCode="abnormal_type" placeholder="请选择问题组别"/>
						</p-form-item>
					</p-col>

					<!--<p-col  :span="24">
						<p-form-item label="不良组名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
							<f-popup
									contextPath="mes-quality"
									v-decorator="['badGroupName', validatorRules.badGroupName]"
									:trigger-change="true"
									org-fields="bad_group_id,bad_group_billno,bad_group_name"
									dest-fields="bad_group_id,bad_group_billno,bad_group_name"
									code="mes_bad_group"
									:add="this.url.add"
									isMultiSelect="popup"
									@oNvisible="oNvisible"
									@callback="popupCallback"/>
						</p-form-item>
					</p-col>-->
					<p-col  :span="24">
						<p-form-item label="类别编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
							<p-input readOnly  v-decorator="[ 'abnormalCode', validatorRules.abnormalCode]" placeholder="请输入异常类别编码"></p-input>
						</p-form-item>
					</p-col>
					<p-col  :span="24">
						<p-form-item label="类别名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
							<p-input  v-decorator="['abnormalType', validatorRules.abnormalType]"  placeholder="请选择异常类别名称"/>
						</p-form-item>
					</p-col>
					<p-col  :span="24">
						<p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
							<j-dict-select-tag  type="radio" v-decorator="['status', validatorRules.status]" :trigger-change="true" dictCode="mes_status" placeholder="请选择状态"/>
						</p-form-item>
					</p-col>
					<p-col  :span="24">
						<p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
							<p-input  v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
						</p-form-item>
					</p-col>
				</p-row>


			</p-form>
		</p-spin>
	</p-modal>
</template>

<script>
	import { httpAction, putAction, api } from '@/api/manage'
	import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
	import pick from 'lodash.pick'
	import { validateDuplicateValue } from '@/utils/util'
	import JDictSelectTag from "@/components/dict/JDictSelectTag"

	export default {
		name: "MesQcExceptionTypesModal",
		mixins: [SubmitOnchangMixin],
		components: {
			JDictSelectTag,
		},
		data () {
			return {
				form: this.$form.createForm(this),
				title: '操作',
				width: 800,
				visible: false,
				model: {},
				labelCol: {
					xs: { span: 24 },
					sm: { span: 5 },
				},
				wrapperCol: {
					xs: { span: 24 },
					sm: { span: 16 },
				},
				confirmLoading: false,
				validatorRules: {
					badGroupName: {rules: [
							{required: true, message: '请输入问题组别!'},
						]},
					abnormalCode: {rules: [
							{required: true, message: '请输入类别编码!'},
						]},
					abnormalType: {rules: [
							{required: true, message: '请输入类别名称!'},
						]},
					status: {rules: [
							{required: true, message: '请输入状态!'},
						]},
					remark: {rules: [
						]},
				},
				url: {
					add: '/mes-quality/mesQcExceptionTypes/add',
					edit: '/mes-quality/mesQcExceptionTypes/edit',
				}
			}
		},
		created () {
		},
		mounted() {
			this.form.setFieldsValue({ status: 1 });
			this.model.status = 1;
		},

		methods: {
			add() {
				this.edit({});
				this.getRuleCodeValue();
				// this.form.setFieldsValue({ status: 1 });
				// this.model.status = 1;
			},
			getRuleCodeValue() {
				let param = {
					commonFormData: this.model,
					rules: [
						{"ruleCode": "mes_err_type_rule", "columnName": "abnormalCode"},
					]
				}

				putAction(api.ruleBaseURL, param).then(res => {
					// 执行成功，获取返回的值，并赋到页面上
					if (res.success) {
						console.log("生成的编码：" + res.data)
						const item = res.data;
						item['status'] = 1;//表单默认值赋值区域
						this.form.setFieldsValue(pick(item, Object.keys(item)))
					}
				})
			},

			edit (record) {
				this.form.resetFields();
				this.model = Object.assign({}, record);
				this.visible = true;
				this.$nextTick(() => {
					this.form.setFieldsValue(pick(this.model,'badGroupBillno','badGroupName','abnormalCode','abnormalType','status','createBy','createTime','updateBy','updateTime','remark'))
				})
			},
			close () {
				this.$emit('close');
				this.visible = false;
			},
			handleOk () {
				const that = this;
				// 触发表单验证
				this.form.validateFields((err, values) => {
					if (!err) {
						that.confirmLoading = true;
						let httpurl = '';
						let method = '';
						let formData = Object.assign(this.model, values);
						this.formData = Object.assign(this.model, values);
						//新增触发
						if(!this.model.id){
							httpurl+=this.url.add;
							method = 'post';
							if(this.EnhanceJS.afterAdd){
								//新增之前
								let Obj = eval(this.EnhanceJS.afterAdd)
								new Obj(this)
							}else{
								console.log("表单提交数据",formData)
								that.modalFrom(httpurl,formData,method)
							}
						}else{
							//编辑触发
							httpurl+=this.url.edit;
							method = 'put';
							if(this.EnhanceJS.afterEdit){
								//编辑之前
								let Obj = eval(this.EnhanceJS.afterEdit)
								new Obj(this)
							}else{
								console.log("表单提交数据",formData)
								that.modalFrom(httpurl,formData,method)
							}
						}
					}

				})
			},
			handleCancel () {
				this.close()
			},
			popupCallback(row){
				this.form.setFieldsValue(pick(row,'badGroupBillno','badGroupName','abnormalCode','abnormalType','status','createBy','createTime','updateBy','updateTime','remark'))
			},


		}
	}
</script>
